{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##  复习"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 计算机基础及python语法基础\n",
    "\n",
    "#### 计算机基础\n",
    "* 计算机的组成\n",
    "* 数的二进制表示\n",
    "* 数值计算中的误差\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "#### python语言相关\n",
    "* 变量类型\n",
    "* 列表、字典、元组\n",
    "* 条件语句、循环语句\n",
    "* 函数及类\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'list'>\n",
      "3\n",
      "[1, 2, 4, 3]\n",
      "[1, 2, 3, 4]\n",
      "[1, 2, 4]\n",
      "[1, 2, 4, 1, 3, 5]\n",
      "1\n",
      "2\n",
      "4\n",
      "<class 'tuple'>\n",
      "<class 'dict'>\n",
      "1\n",
      "<class 'str'> <class 'int'>\n",
      "1 1\n",
      "<class 'str'> <class 'int'>\n",
      "2 2\n",
      "<class 'str'> <class 'int'>\n",
      "3 3\n",
      "{'1': 1, '2': 2, '3': 3}\n",
      "10\n",
      "11\n",
      "12\n",
      "13\n",
      "14\n",
      "15\n",
      "16\n",
      "17\n",
      "18\n",
      "19\n",
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "9\n",
      "7\n"
     ]
    }
   ],
   "source": [
    "#列表\n",
    "a = [1,2,3]\n",
    "b = [1,3,5]\n",
    "print(type(a))\n",
    "print(len(a))\n",
    "a.insert(2,4)\n",
    "print(a)\n",
    "a.sort()\n",
    "print(a)\n",
    "del a[2]\n",
    "print(a)\n",
    "print(a + b)\n",
    "for i in a:\n",
    "    print(i)\n",
    "\n",
    "#元组\n",
    "a = (1,2,3)\n",
    "print(type(a))\n",
    "\n",
    "#字典\n",
    "a = {\"1\":1,\"2\":2,\"3\":3}\n",
    "print(type(a))\n",
    "print(a[\"1\"])\n",
    "\n",
    "for key,val in a.items():\n",
    "    print(type(key),type(val))\n",
    "    print(key,val)\n",
    "\n",
    "#条件语句\n",
    "if len(a) > 2:\n",
    "    print(a)\n",
    "else:\n",
    "    print(b)\n",
    "\n",
    "i = 10\n",
    "while i < 20:\n",
    "    print(i) \n",
    "    i=i+1\n",
    "\n",
    "#循环语句\n",
    "for i in range(10):\n",
    "    if i > 3:\n",
    "        break\n",
    "    print(i)\n",
    "\n",
    "# 函数\n",
    "def f(x):\n",
    "    return x**2 + 2*x +1\n",
    "print(f(2))\n",
    "\n",
    "# 类\n",
    "class test:\n",
    "    a = 3    \n",
    "    b = 4\n",
    "    def __init__(self,a=None,b=None) -> None:\n",
    "        if a is not None:\n",
    "            self.a = a\n",
    "        if b is not None:\n",
    "            self.b = b\n",
    "\n",
    "    def add_a_b(self):\n",
    "        return self.a+self.b \n",
    "\n",
    "tmp = test()\n",
    "print(tmp.add_a_b())\n",
    "\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "#### numpy、scipy及matplotlib\n",
    "* 包的导入\n",
    "* numpy的ndarray的基本使用\n",
    "* 函数及数据的可视化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import scipy as sp\n",
    "from matplotlib import pyplot as plt\n",
    "#具体见课件"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 计算物理基础\n",
    "\n",
    "#### 离散化处理\n",
    "* 积分的离散化及计算（一维、二维）\n",
    "* 微分的离散化及计算（一阶，二阶）\n",
    "* 初值问题的常微分方程\n",
    "  \n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 插值与近似\n",
    "* 一次插值、二次插值、拉格朗日插值\n",
    "* 样条插值\n",
    "* 切比雪夫函数近似\n",
    "* 积分与微分中插值的应用\n",
    "* 微分方程中插值的应用\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.5\n",
      "2.25\n",
      "2.25\n",
      "1.0 + 1.0·T₁(x) + 1.0·T₂(x)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "4.680000000000001"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 插值的一些函数\n",
    "print(np.interp(1.5,[1,2],[1,4]))\n",
    "from scipy.interpolate import  lagrange,CubicSpline\n",
    "p = lagrange([1,2,3],[1,4,9])\n",
    "print(p(1.5))\n",
    "p = CubicSpline([1,2,3],[1,4,9])\n",
    "print(p(1.5))\n",
    "\n",
    "from numpy.polynomial import Chebyshev as T\n",
    "print(T([1,1,1]))\n",
    "T([1,1,1])(1.3)\n",
    "\n",
    "# 积分\n",
    "from scipy.integrate import quad, dblquad\n",
    "from scipy import special\n",
    "result = quad(lambda x: special.jv(2.5,x), 0, 4.5)\n",
    "print(result)\n",
    "\n",
    "area = dblquad(lambda x, y: x*y, 0, 0.5, lambda x: 0, lambda x: 1-2*x)\n",
    "print(area)\n",
    "\n",
    "# 微分\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "r = np.array([0.593311, 0.499786, 0.405665, 0.312793, 0.222815, 0.137167, 0.057062, \\\n",
    "-0.0165038, -0.0827553, -0.14113, -0.191268, -0.233, -0.266334, \\\n",
    "-0.291435, -0.30861, -0.318286, -0.320994, -0.317346, -0.308014, \\\n",
    "-0.293718, -0.275202, -0.253219, -0.228516, -0.201822, -0.173833, \\\n",
    "-0.145206, -0.116543, -0.0883933, -0.0612431, -0.0355129, -0.0115564, \\\n",
    "0.0103399, 0.029956, 0.0471362, 0.0617858, 0.0738674, 0.0833957, \\\n",
    "0.0904327, 0.0950818, 0.0974818, 0.0978011])\n",
    "t = np.arange(1,5.1,0.1)\n",
    "plt.plot(t,r)\n",
    "ddy = (np.roll(r,-1)+ np.roll(r,1) - 2 * r) / 0.1**2\n",
    "plt.plot(t[1:-1],ddy[1:-1])\n",
    "\n",
    "#微分方程\n",
    "from scipy.integrate import solve_ivp,odeint\n",
    "def f(t,y,k):\n",
    "    return [y[1],-k*y[0]]\n",
    "sol = solve_ivp(f,[0,100],[0,4],t_eval=np.arange(0,100,0.1),args=(0.1,))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "#### 迭代\n",
    "* 一元及多元方程求根\n",
    "* 一元及多元函数求极值\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "      converged: True\n",
      "           flag: 'converged'\n",
      " function_calls: 9\n",
      "     iterations: 8\n",
      "           root: (0.6963233908513129+1.4359498641099924j)\n",
      "    fjac: array([[-1.]])\n",
      "     fun: array([0.])\n",
      " message: 'The solution converged.'\n",
      "    nfev: 9\n",
      "     qtf: array([-4.82558438e-12])\n",
      "       r: array([-3.24780793])\n",
      "  status: 1\n",
      " success: True\n",
      "       x: array([-0.39264678])\n",
      "[-0.39264678]\n"
     ]
    }
   ],
   "source": [
    "from scipy.optimize import fsolve\n",
    "from scipy.optimize import root,root_scalar\n",
    "def f(x):\n",
    "    return x**3 - x**2 + 2*x +1 \n",
    "\n",
    "print(root_scalar(f,x0=1+2j,x1=2+3j))\n",
    "print(root(f,x0=0))\n",
    "print(fsolve(f,x0=1))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "#### 矩阵\n",
    "* 矩阵的一般性质\n",
    "* 线性方程组的求解，奇异值分解\n",
    "* 本征系统求解，QR分解\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 9.00719925e+14 -4.50359963e+14 -1.80143985e+15  1.35107989e+15]\n",
      " [-2.40191980e+15  2.70215978e+15  1.80143985e+15 -2.10167983e+15]\n",
      " [ 2.10167983e+15 -4.05323966e+15  1.80143985e+15  1.50119988e+14]\n",
      " [-6.00479950e+14  1.80143985e+15 -1.80143985e+15  6.00479950e+14]]\n",
      "(array([[ 0.        , -0.83666003,  0.48308786,  0.25812035],\n",
      "       [-0.26726124, -0.47809144, -0.8365087 ,  0.01591196],\n",
      "       [-0.53452248, -0.11952286,  0.22375381, -0.80618499],\n",
      "       [-0.80178373,  0.23904572,  0.12966702,  0.53215267]]), array([[-1.49666295e+01, -1.65701970e+01, -1.81737645e+01,\n",
      "        -1.97773319e+01],\n",
      "       [ 0.00000000e+00, -1.19522861e+00, -2.39045722e+00,\n",
      "        -3.58568583e+00],\n",
      "       [ 0.00000000e+00,  0.00000000e+00, -1.71941189e-15,\n",
      "        -5.12511879e-15],\n",
      "       [ 0.00000000e+00,  0.00000000e+00,  0.00000000e+00,\n",
      "        -8.82043219e-16]]))\n",
      "(array([[-0.09184212, -0.83160389,  0.52939495,  0.14050262],\n",
      "       [-0.31812733, -0.44586433, -0.8105844 ,  0.20725087],\n",
      "       [-0.54441254, -0.06012478,  0.03298396, -0.8360096 ],\n",
      "       [-0.77069775,  0.32561478,  0.2482055 ,  0.48825611]]), array([3.51399637e+01, 2.27661021e+00, 8.80118491e-16, 4.41188001e-17]), array([[-0.42334086, -0.47243254, -0.52152422, -0.57061589],\n",
      "       [ 0.72165263,  0.27714165, -0.16736932, -0.6118803 ],\n",
      "       [ 0.5427818 , -0.66899815, -0.29034911,  0.41656546],\n",
      "       [ 0.0734024 , -0.50243554,  0.78466387, -0.35563073]]))\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import scipy as sp\n",
    "from matplotlib import pyplot as plt\n",
    "from scipy.linalg import inv,qr,svd\n",
    "\n",
    "a = np.arange(0,16).reshape(4,4)\n",
    "print(inv(a))\n",
    "\n",
    "print(qr(a))\n",
    "\n",
    "print(svd(a))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 数值计算在物理学中的应用\n",
    "\n",
    "#### 单粒子问题\n",
    "* 经典一维谐振子系统\n",
    "* 单粒子量子系统的求解\n",
    "\n",
    "#### 多粒子系统\n",
    "* 分子动力学模拟\n",
    "* 蒙特卡洛积分\n",
    "* 蒙特卡洛模拟"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.9.12 ('base')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "36b5677e7909a0b3d34004c01c225c0a9ffb16d3aacb819783bafe2786ef65fe"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
